package aaTesterOnlyCode;

import java.io.IOException;
import java.net.URLEncoder;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;

/**
 * Fetch Images from Google about specific things and download them
 * 
 * @author GOXR3PLUS
 *
 */
public class InternetImageFetcher {
	
	String encoding = "UTF-8";
	
	/**
	 * Constructor
	 */
	public InternetImageFetcher() {
		
		try {
			String url = "https://www.google.com/search?q=" + URLEncoder.encode("Albatroz", encoding) + "&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiay-ndpYTWAhVlJ8AKHRB8Ag0Q_AUICigB";
			
			Document doc = Jsoup.connect(url).get();
			System.out.println("Base URL " + doc.baseUri() + "\n");
			System.out.println(doc.body());
			
			//Let's scan all the available images
			doc.getElementsByClass("rg_ic rg_i").stream().limit(3).forEach(image -> {
				Attributes imageSRC = image.attributes();
				System.out.println("src  " + imageSRC);
			});
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//		String sourceData = "";
		//		
		//		String base64Image = sourceData.split(",")[1];
		//		byte[] imageBytes = DatatypeConverter.parseBase64Binary(base64Image);
		//		
		//		try {
		//			BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageBytes));
		//			
		//			// write the image to a file
		//			File outputfile = new File("image.jpeg");
		//			ImageIO.write(img, "png", outputfile);
		//		} catch (IOException e) {
		//			e.printStackTrace();
		//		}
		
	}
	
	public static void main(String[] args) {
		
		new InternetImageFetcher();
	}
}
